//*********************************************************
//
//    Copyright (c) Microsoft. All rights reserved.
//    This code is licensed under the Apache License, Version 2.0.
//    THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
//    ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
//    IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
//    PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//*********************************************************

namespace Microsoft.Research.ScientificWorkflow.ManagementStudioControls.RegistryManagerModule
{
    using System;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Input;
    using Microsoft.Research.ScientificWorkflow.ManagementStudioModels;
    using Microsoft.Research.ScientificWorkflow.TridentUtilities;

    /// <summary>
    /// Interaction logic for WorkflowTree.xaml
    /// </summary>
    public partial class WorkflowTree : UserControl
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="WorkflowTree"/> class.
        /// </summary>
        public WorkflowTree()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Gets or sets the model.
        /// </summary>
        /// <value>The model.</value>
        public WorkflowLeftPanePresenter Model
        {
            get { return this.DataContext as WorkflowLeftPanePresenter; }
            set { this.DataContext = value; }
        }

        /// <summary>
        /// The selected item changed event on the treeview.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The event arguments.</param>
        private void OnSelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
        {
            try
            {
                this.Model.ItemSelectedCommand.Execute(e.NewValue as BaseRegMgrModel);
            }
            catch (TridentCustomException ex)
            {
                TridentErrorHandler.HandleTridentCustomExceptionInUI(ex);
            }
            catch (Exception ex)
            {
                TridentErrorHandler.HandleKnownExceptionsInUI(ex);
            }
        }

        /// <summary>
        /// Event handler for the PreviewMouseDown event on the category treeview. This event is needed
        /// because the selection changed event does not fire when alternating between 2 controls.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The event arguments.</param>
        private void Grid_PreviewMouseDown(object sender, MouseButtonEventArgs e)
        {
            if (!this.Model.IsSelected && workflowTreeview.SelectedItem != null)
            {
                Grid selectedGrid = sender as Grid;
                BaseRegMgrModel model = selectedGrid.DataContext as BaseRegMgrModel;

                // If the mousedown happens on the currently selected item then fire the selected command.
                if (workflowTreeview.SelectedItem == model)
                {
                    this.Model.ItemSelectedCommand.Execute(model);
                }
            }
        }
    }
}
